//
//  HXJobWeekDetailsViewController.m
//  KD
//
//  Created by ifly on 16/5/27.
//  Copyright © 2016年 Whx. All rights reserved.
//

#define DateLabelWith 50
#define Gap 10
#define BtnWith HXSCREEN_WIDTH - 20 - DateLabelWith
#define BtnHeight 38

#import "HXJobWeekDetailsViewController.h"
//#import "HXPictureGatherViewController.h"
#import "HXSitePictureViewController.h"

@interface HXJobWeekDetailsViewController ()
@property(nonatomic,strong) UILabel *dateLabel;
@property(nonatomic,weak) UIView *amView;
@property(nonatomic,weak) UIView *pmView;
@property(nonatomic,strong) UILabel *amLabel;
@property(nonatomic,strong) UILabel *pmLabel;
@property(nonatomic,strong) UIButton *amAddBtn;
@property(nonatomic,strong) UIButton *pmAddBtn;

@end

#warning give--up--use-----



@implementation HXJobWeekDetailsViewController

- (void)setModel:(HXJopRecordListModel *)model{
    _model = model;
    if ([GlobalUtil isNilValue:model.CreateDate]) {
        self.dateLabel.text = [NSString stringWithFormat:@"%@ ",model.Week];
    }else{
        self.dateLabel.text = [NSString stringWithFormat:@"%@      %@",model.CreateDate,model.Week];
    }
//    self.amView.backgroundColor = HXRandomColor;
//    self.pmView.backgroundColor = HXRandomColor;
    [self.amView removeFromSuperview];
    [self.pmView removeFromSuperview];
    [self addAMView];
    [self addPMView];
//    [self addLineView];
    self.myHeight = CGRectGetMaxY(self.pmView.frame) + Gap;
}

-(void)addAMView{
    UIView *amView = [[UIView alloc]init];
    [self.view addSubview:amView];
    self.amView = amView;
    [self.amView addSubview:self.amLabel];
    if ([self.model.isMy isEqualToString:@"yes"] || [self.model.Work_AMText isEqualToString:@"-1"]) {//自己添加的模型 这是只需要一个添加按钮
        [self.amView addSubview:self.amAddBtn];
        self.amView.frame = CGRectMake(0, CGRectGetMaxY(self.dateLabel.frame) + Gap, HXSCREEN_WIDTH, BtnHeight);
        return;
    }
    CGFloat siteY = 0;//工地现场的高度
    for (HXJopRecordSiteListModel *amModel in self.model.AMList) {
        if([amModel.TaskType isEqualToString:@"1"]){//工地工作
            CGRect rect = CGRectMake(DateLabelWith + Gap,siteY, BtnWith, BtnHeight);
            siteY = siteY + BtnHeight + Gap;//改变View高度 是下一个按钮的y
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            btn.layer.borderWidth = 1;
            btn.layer.borderColor = MAINCOLOR.CGColor;
            btn.layer.cornerRadius = 8;
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
            [btn addTarget:self action:@selector(goSiteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = [amModel.TaskID integerValue];
            [btn setFrame:rect];
            [btn setTitle:[NSString stringWithFormat:@"%@%@",amModel.SiteName,amModel.HasPunish] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
            [self.amView addSubview:btn];
            btn.backgroundColor = MAINCOLOR;
        }
    }
    CGFloat otherY = 0;//文字工作的高度
    for (HXJopRecordSiteListModel *amModel in self.model.AMList) {
        if ([amModel.TaskType isEqualToString:@"2"]) {//文字工作  只有一个
            CGSize size= [self SizeWithString:[NSString stringWithFormat:@"%@%@",amModel.SiteName,amModel.HasPunish]];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.layer.borderWidth = 1;
            btn.layer.borderColor = MAINCOLOR.CGColor;
            btn.layer.cornerRadius = 8;
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
            NSString *str = [NSString stringWithFormat:@"%@%@",amModel.SiteName,amModel.HasPunish];
            if (![GlobalUtil isNilValue:str]) {
                [btn setTitle:str forState:UIControlStateNormal];
            }else{
                [btn setTitle:@"暂无" forState:UIControlStateNormal];
            }
            [btn setTitleColor:MAINCOLOR forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
            otherY = size.height;
            if (otherY < BtnHeight) otherY = BtnHeight;
            [btn setFrame:CGRectMake(DateLabelWith + Gap, siteY, BtnWith, otherY)];
//            btn.backgroundColor = [UIColor yellowColor];
            [self.amView addSubview:btn];
        }
    }
    self.amView.frame = CGRectMake(0, CGRectGetMaxY(self.dateLabel.frame)+Gap, SCREEN_WIDTH, siteY + otherY  + Gap);
}

-(void)addPMView{
    UIView *pmView = [[UIView alloc]init];
    [self.view addSubview:pmView];
    self.pmView = pmView;
    [self.pmView addSubview:self.pmLabel];
    if ([self.model.isMy isEqualToString:@"yes"] || [self.model.Work_PMText isEqualToString:@"-1"]) {//自己添加的模型 这是只需要一个添加按钮
        [self.pmView addSubview:self.pmAddBtn];
        self.pmView.frame = CGRectMake(0, CGRectGetMaxY(self.amView.frame) + Gap, HXSCREEN_WIDTH, BtnHeight);
        return;
    }
    CGFloat siteY = 0;//工地现场的高度
    for (HXJopRecordSiteListModel *pmModel in self.model.PMList) {
        if([pmModel.TaskType isEqualToString:@"1"]){//工地工作
            CGRect rect = CGRectMake(DateLabelWith + Gap,siteY, BtnWith, BtnHeight);
            siteY = siteY + BtnHeight + Gap;//改变View高度 是下一个按钮的y
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            btn.layer.borderWidth = 1;
            btn.layer.borderColor = MAINCOLOR.CGColor;
            btn.layer.cornerRadius = 8;
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
            [btn addTarget:self action:@selector(goSiteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = [pmModel.TaskID integerValue];
            [btn setFrame:rect];
            [btn setTitle:[NSString stringWithFormat:@"%@%@",pmModel.SiteName,pmModel.HasPunish] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
            [self.pmView addSubview:btn];
            btn.backgroundColor = MAINCOLOR;
        }
    }
    CGFloat otherY = 0;//文字工作的高度
    for (HXJopRecordSiteListModel *pmModel in self.model.PMList) {
        if ([pmModel.TaskType isEqualToString:@"2"]) {//文字工作  只有一个
            CGSize size= [self SizeWithString:[NSString stringWithFormat:@"%@%@",pmModel.SiteName,pmModel.HasPunish]];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.layer.borderWidth = 1;
            btn.layer.borderColor = MAINCOLOR.CGColor;
            btn.layer.cornerRadius = 8;
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
            NSString *str = [NSString stringWithFormat:@"%@%@",pmModel.SiteName,pmModel.HasPunish];
            if (![GlobalUtil isNilValue:str]) {
                [btn setTitle:str forState:UIControlStateNormal];
            }else{
                [btn setTitle:@"暂无" forState:UIControlStateNormal];
            }
            [btn setTitleColor:MAINCOLOR forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
            otherY = size.height;
            if (otherY < BtnHeight) otherY = BtnHeight;
            [btn setFrame:CGRectMake(DateLabelWith + Gap, siteY, BtnWith, otherY)];
//            btn.backgroundColor = [UIColor yellowColor];
            [self.pmView addSubview:btn];
        }
    }
    self.pmView.frame = CGRectMake(0, CGRectGetMaxY(self.amView.frame)+Gap, HXSCREEN_WIDTH, siteY + otherY  + Gap);
}

-(void)addLineView{
    UIView *lineView = [[UIView alloc]init];
    lineView.frame = CGRectMake(10, CGRectGetMaxY(self.pmView.frame) +1, HXSCREEN_WIDTH - 20, 1);
    [self.view addSubview:lineView];
    lineView.backgroundColor = [UIColor lightGrayColor];
}

-(void)goSiteBtnClick:(UIButton *)btn{
    
    HXSitePictureViewController *Vc = [HXSitePictureViewController CreateSitePictureVCWithTaskID:[NSString stringWithFormat:@"%ld",(long)btn.tag] withIsAdd:NO withIsListOrRiZhiPush:YES withIsExistTaskId:NO withLocalDataDic:@{}];
    [self.superVc.navigationController pushViewController:Vc animated:YES];
    //[self.superVc goSiteWithTaskId:[NSString stringWithFormat:@"%ld",(long)btn.tag] createDate:self.model.CreateDate];
}

-(CGSize)SizeWithString:(NSString*)str
{
    UIFont *strfont = [UIFont systemFontOfSize:15];
    NSDictionary *dict = @{NSFontAttributeName:strfont};
    CGSize maxSize = CGSizeMake(BtnWith, MAXFLOAT);
    CGSize size = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size;
}

#pragma mark - 懒加载
- (UILabel *)dateLabel{
    if (_dateLabel == nil) {
        _dateLabel = [[UILabel alloc]init];
        _dateLabel.frame = CGRectMake(Gap, 0, HXSCREEN_WIDTH - 2*Gap, 40);
        _dateLabel.layer.cornerRadius = 5;
        _dateLabel.layer.masksToBounds = YES;
        _dateLabel.textColor = [UIColor blackColor];
        _dateLabel.font = [UIFont systemFontOfSize:16];
        _dateLabel.backgroundColor = [UIColor lightGrayColor];
        self.dateLabel = _dateLabel;
        [self.view addSubview:_dateLabel];
    }
    return _dateLabel;
}

- (UILabel *)amLabel{
    if (_amLabel == nil) {
        _amLabel = [[UILabel alloc]init];
        _amLabel.font = [UIFont systemFontOfSize:15];
        _amLabel.backgroundColor = MAINCOLOR;
        _amLabel.text = @"上午";
        _amLabel.frame = CGRectMake(10, 0, 40, BtnHeight);
        _amLabel.textAlignment = NSTextAlignmentCenter;
        self.amLabel = _amLabel;
    }
    return _amLabel;
}

- (UILabel *)pmLabel{
    if (_pmLabel == nil) {
        _pmLabel = [[UILabel alloc]init];
        _pmLabel.font = [UIFont systemFontOfSize:15];
        _pmLabel.backgroundColor = MAINCOLOR;
        _pmLabel.text = @"下午";
        _pmLabel.frame = CGRectMake(10, 0, 40, BtnHeight);
        _pmLabel.textAlignment = NSTextAlignmentCenter;
        self.pmLabel = _pmLabel;
    }
    return _pmLabel;
}

-(UIButton *)amAddBtn{
    if (_amAddBtn == nil) {
        _amAddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [ _amAddBtn setFrame:CGRectMake(DateLabelWith + Gap, 0, BtnWith, BtnHeight)];
        _amAddBtn.layer.borderWidth = 1;
        _amAddBtn.layer.borderColor = MAINCOLOR.CGColor;
        [_amAddBtn setTitle:@"暂无" forState:UIControlStateNormal];
        [_amAddBtn setTitleColor:MAINCOLOR forState:UIControlStateNormal];
        _amAddBtn.layer.cornerRadius = 8;
        _amAddBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    }
    return _amAddBtn;
}

- (UIButton *)pmAddBtn{
    if (_pmAddBtn == nil) {
        _pmAddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [ _pmAddBtn setFrame:CGRectMake(DateLabelWith + Gap, 0, BtnWith, BtnHeight)];
        _pmAddBtn.layer.borderWidth = 1;
        _pmAddBtn.layer.borderColor = MAINCOLOR.CGColor;
        [_pmAddBtn setTitle:@"暂无" forState:UIControlStateNormal];
        [_pmAddBtn setTitleColor:MAINCOLOR forState:UIControlStateNormal];
        _pmAddBtn.layer.cornerRadius = 8;
        _pmAddBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    }
    return _pmAddBtn;
}

@end
